<?php

namespace App\Http\Controllers\Admin;


use App\Models\Order;
use App\Models\OrderDetail;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class OrderController extends Controller
{
    /*
     * @商品订单的首页
     */
    public function index(Request $request){
        if($request->isMethod('post')) {
            $name=$request->input('username');
            $paginate = 15;
            $orders = Order::leftJoin('shops', 'shops.id', '=', 'orders.shop_id')
                ->leftJoin('users', 'users.id', '=', 'orders.user_id')
                ->select('orders.*', 'users.name', 'shops.shop_name')
                ->where('orders.is_active','=','0')
                ->where(function($query)use($name){
                        $query ->where('name','like','%'. $name.'%')
                            ->orWhere(function ($query)use($name){
                                $query->where('order_num','like','%'. $name. '%');
                            });
                })->paginate($paginate);
//            统计页数
            $count=Order::leftJoin('shops', 'shops.id', '=', 'orders.shop_id')
                ->leftJoin('users', 'users.id', '=', 'orders.user_id')
                ->select('orders.*', 'users.name', 'shops.shop_name')
                ->where('orders.is_active','=','0')
                ->where(function($query)use($name){
                    $query ->where('name','like','%'. $name.'%')
                        ->orWhere(function ($query)use($name){
                            $query->where('order_num','like','%'. $name. '%');
                        });
                })->count();
//            dd($count);
            $total_page = ceil($count/$paginate);//共多少页

            return view("admin.order",['orders'=>$orders,'name'=>$name])->with('total_page',$total_page);
        }
        //注意参数post请求表单的username,  get请求post的name
        $name=$request->input('name');
        $paginate=15;
        $orders = Order::leftJoin('shops', 'shops.id', '=', 'orders.shop_id')
            ->leftJoin('users', 'users.id', '=', 'orders.user_id')
            ->select('orders.*', 'users.name', 'shops.shop_name')
            ->where('orders.is_active','=','0')
            ->where(function($query)use($name){
                $query ->where('name','like','%'. $name.'%')
                    ->orWhere(function ($query)use($name){
                        $query->where('order_num','like','%'. $name. '%');
                    });
            })->paginate($paginate);
//        统计页数
        $count=Order::leftJoin('shops', 'shops.id', '=', 'orders.shop_id')
            ->leftJoin('users', 'users.id', '=', 'orders.user_id')
            ->select('orders.*', 'users.name', 'shops.shop_name')
            ->where('orders.is_active','=','0')
            ->where(function($query)use($name){
                $query ->where('name','like','%'. $name.'%')
                    ->orWhere(function ($query)use($name){
                        $query->where('order_num','like','%'. $name. '%');
                    });
            })->count();
        $total_page = ceil($count/$paginate);//共多少页
        //dd($orders);
        return view("admin.order",['orders'=>$orders,'name'=>$name])->with('total_page',$total_page);
    }
    /*
     * @订单的详情页面
     */
    public function show($id){
//        订单显示

        $orders=Order::leftJoin('shops','shops.id','=','orders.shop_id')
            ->leftJoin('users','users.id','=','orders.user_id')
            ->select('orders.*','users.name','shops.shop_name')
            ->where('orders.id','=',$id)
            ->get();
//        dd($orders);
//        订单的商品列表详情
        $paginate=10;
        $glists=OrderDetail::leftJoin('goods_nexus','order_detail.rfid','=','goods_nexus.rfid')
            ->leftJoin('goods','goods.id','=','goods_nexus.goods_id')
            ->select('order_detail.*','goods.name','goods.product_code','goods.price')
            ->where('order_detail.order_id','=',$id)
            ->paginate($paginate);
//        dd($lists);
        $count=OrderDetail::leftJoin('goods_nexus','order_detail.rfid','=','goods_nexus.rfid')
            ->leftJoin('goods','goods.id','=','goods_nexus.goods_id')
            ->select('order_detail.*','goods.name','goods.product_code','goods.price')
            ->where('order_detail.order_id','=',$id)->count();
        $total_page = ceil($count/$paginate);//共多少页
        return view('admin.order_show')
            ->with('orders',$orders[0])
            ->with( 'glists',$glists)->with('total_page',$total_page);
    }

     /*
      * @订单的删除====>假删除
      */
     public function status(Request $request){
         $is_show=$request->is_show;
         $id = $request->id;
         if($is_show==0){
             $is_show=1;
         }
         $res = Order::find($id)->update(['is_active' => $is_show]);

         return $is_show;
     }
}
